package com.mall.dao;

import com.mall.domain.Order;
import com.mall.domain.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 订单的持久层接口
 *
 * @author : liangY
 * @date : 2021-01-12 23:19
 */
@Repository
public interface IOrderDao {

    /**
     * 查询所有订单
     *
     * @param page 分页实体类
     * @return
     */
    @Select("select * from tb_order where order_code like #{page.query} and flag = 0 limit #{page.num},#{page.size}")
    List<Order> findAll(@Param("page") Page page);

    /**
     * 查询所有订单数量
     *
     * @return
     */
    @Select("select count(*) from tb_order")
    Integer findTotal();

    /**
     * 更新订单
     *
     * @param order 订单实体类
     * @return
     */
    @Update("update tb_order set status=#{status}," +
            "updated=#{updated}," +
            "consign_time=#{consign_time}," +
            "shipping_name=#{shipping_name},shipping_code=#{shipping_code},flag=#{flag} where id=#{id}")
    Integer update(Order order);

    /**
     * 添加订单
     *
     * @param order 订单实体类
     * @return
     */
    @Insert("insert into tb_order values(#{id},#{order_code},#{payment_money}," +
            "#{payment_type}," +
            "#{status},#{created}," +
            "#{updated},#{consign_time}," +
            "#{shipping_name},#{shipping_code},#{member_id}," +
            "#{buyer_msg},#{address_id}," +
            "#{flag})")
    @SelectKey(keyColumn = "goods_id", keyProperty = "goods_id", resultType = Integer.class,
            before = false, statement = {"select last_insert_id()"})
    Integer add(Order order);

    /**
     * 删除订单
     *
     * @param id
     * @return
     */
    @Update("update tb_order set flag = 1 where id = #{id}")
    Integer del(Integer id);
}
